// store/modules/auth.js
export default {
  namespaced: true,
  state: () => ({
    user: null,
    token: null
  }),
  getters: {
    isAuthenticated: state => !!state.token,
    userInfo: state => state.user || {
      username: '用户名',
      avatar: null
    }
  },
  mutations: {
    SET_USER(state, user) {
      state.user = user
    },
    SET_TOKEN(state, token) {
      state.token = token
    }
  },
  actions: {
    async login({ commit }, credentials) {
      const response = await api.login(credentials)
      commit('SET_USER', response.user)
      commit('SET_TOKEN', response.token)
    },
    async logout({ commit }) {
      await api.logout()
      commit('SET_USER', null)
      commit('SET_TOKEN', null)
    },
    async fetchUser({ commit }) {
      const user = await api.getUserInfo()
      commit('SET_USER', user)
    }
  }
}